<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>博客列表</title>
        <link rel="stylesheet" href="css/conmmon.css">
        <link rel="stylesheet" href="css/blog_list.css">
        <script src="js/jquery.min.js"></script>
        <script src="js/logout.js"></script>
        <style>
            .search-container {
                display: flex;
                align-items: center;
            }

            #searchBox {
                width: 300px;
                height: 30px;
                border: 1px solid #ccc;
                padding: 0 10px;
            }

            #search-btn {
                height: 33px;
                width: 50px;
                background-color: #958c3a;
                color: white;
                border: none;
                cursor: pointer;
            }

            .result-container {
                margin-top: 20px;
            }
        </style>
    </head>

    <body>
        <!-- 导航栏 -->
        <div class="nav">
            <img src="img/logo33.png" alt="">
            <span class="title">爱搜blog</span>
            <!-- 用来占据中间位置 -->
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <span class="spacer">
                <span class="search-container">
                    <input type="text" id="searchBox" placeholder="搜索想看的文章">&nbsp;
                    <button onclick="submitSearch()" id="search-btn">搜索</button>
                    <ul id="result-list"></ul>
                </span>
            </span>

            </span>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="blog_list.html">主页</a>
            <a href="blog_add.html ">写博客</a>
            <a id="AfterSearch" href="myblog_list.html">gotoMyBlog</a>
            <a href="javascript:logout()">注销</a>
        </div>
        <!-- 版心 -->
        <div class="container">
            <!-- 左侧个人信息 -->
            <div class="container-left" style="display: none;">
                <div class="card">
                    <img src="img/avatar.png" class="avtar" alt="">
                    <h3>小可爱</h3>
                    <a href="http:www.github.com">github 地址</a>
                    <div class="counter">
                        <span>文章</span>
                        <span>分类</span>
                    </div>
                    <div class="counter">
                        <span>2</span>
                        <span>1</span>
                    </div>
                </div>
            </div>
            <!-- 右侧内容详情 -->
            <div id="artlist" class="container-right" style="margin-left: 9%;">

            </div>

            <div id="searchRequest" class="container-right" style="margin-left: 9%;">

            </div>
        </div>

        <script>

            function submitSearch() {
                var searchBox = jQuery("#searchBox");
                var searchValue = searchBox.val();
                // 创建jQuery ajax对象
                var xhr = jQuery.ajax({
                    url: "/art/search",
                    type: "POST",
                    contentType: 'application/x-www-form-urlencoded',
                    data: { "keyword": searchValue },
                    success: function (res) {
                        if (res.code == 200 && res.data.length > 0) {
                            jQuery("#artlist").hide();
                            // for 循环拼接div 最终放到 artlist 元素中
                            var arthtml = "";
                            for (var i = 0; i < res.data.length; i++) {
                                var art = res.data[i];
                                arthtml += '<div class="blog">';
                                arthtml += '<div class="title">' + art.title + '</div>';
                                arthtml += '<div class="date">' + art.createtime + '&nbsp;&nbsp;&nbsp; 热度🔥：' + art.upvote_count + '</div>';
                                arthtml += '<div class="desc">';
                                arthtml += art.content;
                                arthtml += '</div>';
                                arthtml += '<a href="blog_content.html?id=' + art.id
                                    + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                                arthtml += '<a href="blog_edit.html?id=' + art.id
                                    + '" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
                                arthtml += '<a href="javascript:del(' + art.id + ')" class="detail">删除 &gt;&gt;</a>';
                                arthtml += '</div>';
                            }
                            jQuery('#searchRequest').html(arthtml);
                            jQuery("#searchRequest").show();
                            jQuery("#AfterSearch").show();
                        } else {
                            alert("抱歉没有搜索到您想要的内容！");
                        }
                    },
                    error: function (xhr, status, error) {
                        console.error("请求失败：" + error);
                    }
                });
            }

            function getMyArtList() {
                jQuery.ajax({
                    url: "/art/mylist",
                    type: "POST",
                    data: {},
                    success: function (res) {
                        if (res.code == 200 && res.data.length > 0) {
                            jQuery("#searchRequest").hide();
                            jQuery("#AfterSearch").hide();
                            // for 循环拼接div 最终放到 artlist 元素中
                            var arthtml = "";
                            for (var i = 0; i < res.data.length; i++) {
                                var art = res.data[i];
                                arthtml += '<div class="blog">';
                                arthtml += '<div class="title">' + art.title + '</div>';
                                arthtml += '<div class="date">' + art.createtime + '&nbsp;&nbsp;&nbsp; 热度🔥：' + art.upvote_count + '</div>';
                                arthtml += '<div class="desc">';
                                arthtml += art.content;
                                arthtml += '</div>';
                                arthtml += '<a href="blog_content.html?id=' + art.id
                                    + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                                arthtml += '<a href="blog_edit.html?id=' + art.id
                                    + '" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
                                arthtml += '<a href="javascript:del(' + art.id + ')" class="detail">删除 &gt;&gt;</a>';
                                arthtml += '</div>';
                            }
                            jQuery('#artlist').html(arthtml);
                        } else {
                            //暂无文章
                            jQuery("#artlist").html("<h2 style='margin-left:100px; margin-top='100px'>暂无文章</h2>");
                        }
                    }
                });
            }
            getMyArtList();

            function del(id) {
                if (confirm("确认删除？")) {
                    jQuery.ajax({
                        url: "/art/del",
                        type: "POST",
                        data: { "id": id },
                        success: function (res) {
                            if (res.code == 200 && res.data == 1) {
                                alert("恭喜，删除成功！");
                                //刷新当前页面
                                location.href = location.href;
                            } else {
                                alert("抱歉，操作失败，请重试！" + res.msg);
                            }
                        }
                    });
                }
            }
        </script>
    </body>

</html>